package com.example.service.impl;

import com.example.constant.RedisConstance;
import com.example.util.NumberUtils;
import com.example.util.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 流量统计
 * @Author: aimi
 * @Create: 2024-06-08
 **/
@Service
public class FlowServiceImpl {
    @Resource
    private RedisUtil redisUtil;

    public Map<String,Object> getAdminFlowMap(){
        HashMap<String, Object> flowMap = new HashMap<>();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = now.format(formatter);
        String todayVisitKey = RedisConstance.FLOW_VISIT_KEY+today;
        String todayRequestKey = RedisConstance.FLOW_REQUEST_KEY+today;

        Integer todayVisitNum = NumberUtils.toInt(redisUtil.get(todayVisitKey));
        flowMap.put("todayVisit",todayVisitNum != null ? todayVisitNum : 0);
        Integer todayRequestNum = NumberUtils.toInt(redisUtil.get(todayRequestKey));
        flowMap.put("todayRequestNum",todayRequestNum != null ? todayRequestNum : 0);

        LocalDate yesterdayDate = now.plusDays(-1);
        String yesterday = yesterdayDate.format(formatter);
        String yesterdayVisitKey = RedisConstance.FLOW_VISIT_KEY+yesterday;
        String yesterdayRequestKey = RedisConstance.FLOW_REQUEST_KEY+yesterday;

        Integer yesterdayVisitNum = NumberUtils.toInt(redisUtil.get(yesterdayVisitKey));
        flowMap.put("yesterdayVisit",yesterdayVisitNum != null ? yesterdayVisitNum : 0);
        Integer yesterdayRequestNum = NumberUtils.toInt(redisUtil.get(yesterdayRequestKey));
        flowMap.put("yesterdayRequestNum",yesterdayRequestNum != null ? yesterdayRequestNum : 0);

        return flowMap;
    }

    public void saveFlow(String ip){
        LocalDate todayDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = todayDate.format(formatter);
        String ipSetKey = RedisConstance.FLOW_IP_KEY+today;
        Set<Object> ipSet = redisUtil.setMembers(ipSetKey);
        if(ipSet == null || !ipSet.contains(ip)){
            redisUtil.setAdd(ipSetKey,ip);
            String todayVisitKey = RedisConstance.FLOW_VISIT_KEY+today;
            if(!redisUtil.hasKey(todayVisitKey)){
                redisUtil.set(todayVisitKey,1);
            }else{
                String todayVisitStr = redisUtil.get(todayVisitKey).toString();
                int todayVisitNum = NumberUtils.stringToInt(todayVisitStr);
                redisUtil.set(todayVisitKey,todayVisitNum+1);
            }
        }

        String todayRequestKey = RedisConstance.FLOW_REQUEST_KEY+today;
        if (!redisUtil.hasKey(todayRequestKey)) {
            redisUtil.set(todayRequestKey,1);
        }else{
            String todayRequestStr = redisUtil.get(todayRequestKey).toString();
            int todayRequestNum = NumberUtils.stringToInt(todayRequestStr);
           redisUtil.set(todayRequestKey, todayRequestNum+1);
        }
    }
}